package com.rdc.qingkou.ui;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.percent.PercentRelativeLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.adapter.MyFragmentStatePagerAdapter;
import com.rdc.qingkou.utils.ViewUtil;
import com.rdc.qingkou.view.fragment.ShareViewFragmentone;
import com.rdc.qingkou.view.fragment.ShareViewFragmentwo;
import com.rdc.qingkou.widget.GalleryPageTransformer;
import com.rdc.qingkou.widget.MyViewPager;

import net.arvin.socialhelper.entities.QQShareEntity;
import net.arvin.socialhelper.entities.WBShareEntity;
import net.arvin.socialhelper.entities.WXShareEntity;

import java.util.ArrayList;
import java.util.List;




public class ShareActivity extends AppCompatActivity {

    private TextView qq;
    private TextView weixin;
    private TextView weibo;
    private MyViewPager viewPager;
    private MyFragmentStatePagerAdapter adapter;
    private PercentRelativeLayout mRootLayout;
    private ImageButton back;
    private ImageButton download;
    private ImageView mDot1;
    private ImageView mDot2;
    private int mPosition;
    private String text;
    private  String url;
    private String mShareImage;
    private  ShareViewFragmentone fragmentone;
    private ShareViewFragmentwo fragmentwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        setContentView(R.layout.activity_share);
        initViews();




    }


    private void initViews() {
        text = getIntent().getStringExtra("text");
        url = getIntent().getStringExtra("image");

        mDot1 = (ImageView)findViewById(R.id.iv_dot1);
        mDot2 = (ImageView)findViewById(R.id.iv_dot2);
        back = (ImageButton) findViewById(R.id.share_back_imageButton);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        download = (ImageButton) findViewById(R.id.share_download_imageButton);

        qq = (TextView) findViewById(R.id.share_qq_textView);
        Drawable drawable_qq = getResources().getDrawable(R.drawable.button_qq, null);
        drawable_qq.setBounds(0, 0, 120, 120);
        qq.setCompoundDrawables(null, drawable_qq, null, null);
        qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
           if (mPosition==0){
               mShareImage = ViewUtil.getUrl(fragmentone.getView(),ShareActivity.this);
           }else {
               mShareImage = ViewUtil.getUrl(fragmentwo.getView(),ShareActivity.this);
           }
                MyApplication.getHelper().shareQQ(ShareActivity.this, new net.arvin.socialhelper.callback.SocialShareCallback() {
                    @Override
                    public void shareSuccess() {
                        Toast.makeText(ShareActivity.this,"分享成功",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void socialError(String msg) {
                        Toast.makeText(ShareActivity.this,"分享失败",Toast.LENGTH_SHORT).show();

                    }
                },QQShareEntity.createImageInfo(mShareImage,"轻叩"));

            }
        });


        weixin = (TextView) findViewById(R.id.share_weixin_textView);
        Drawable drawable_weixin = getResources().getDrawable(R.drawable.button_weixin, null);
        drawable_weixin.setBounds(0, 0, 125, 125);
        weixin.setCompoundDrawables(null, drawable_weixin, null, null);
        weixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPosition==0){
                    mShareImage = ViewUtil.getUrl(fragmentone.getView(),ShareActivity.this);
                }else {
                    mShareImage = ViewUtil.getUrl(fragmentwo.getView(),ShareActivity.this);
                }
                Log.e("TAG", "onClick: "+mShareImage );
                MyApplication.getHelper().shareWX(ShareActivity.this, new net.arvin.socialhelper.callback.SocialShareCallback() {
                    @Override
                    public void shareSuccess() {
                        Toast.makeText(ShareActivity.this,"分享成功",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void socialError(String msg) {
                        Toast.makeText(ShareActivity.this,"分享失败",Toast.LENGTH_SHORT).show();

                    }
                }, WXShareEntity.createImageInfo(true,mShareImage));
            }
        });

        weibo = (TextView) findViewById(R.id.share_weibo_textView);
        Drawable drawable_weibo = getResources().getDrawable(R.drawable.button_weibo, null);
        drawable_weibo.setBounds(0, 0, 120, 120);
        weibo.setCompoundDrawables(null, drawable_weibo, null, null);
        weibo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPosition==0){
                    mShareImage = ViewUtil.getUrl(fragmentone.getView(),ShareActivity.this);
                }else {
                    mShareImage = ViewUtil.getUrl(fragmentwo.getView(),ShareActivity.this);
                }

                        MyApplication.getHelper().shareWB(ShareActivity.this, new net.arvin.socialhelper.callback.SocialShareCallback() {
                            @Override
                            public void shareSuccess() {
                                Toast.makeText(ShareActivity.this,"分享成功",Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void socialError(String msg) {
                                Toast.makeText(ShareActivity.this,"分享失败",Toast.LENGTH_SHORT).show();

                           }
                        }, WBShareEntity.createImageTextInfo(mShareImage,"轻叩"));
                    }
                });




        mRootLayout = (PercentRelativeLayout) findViewById(R.id.share_layout);
        mRootLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return viewPager.dispatchTouchEvent(motionEvent);
            }
        });

        viewPager = (MyViewPager) findViewById(R.id.share_viewpager);

        List<Fragment> list = new ArrayList<>();
         fragmentone = new ShareViewFragmentone();
        fragmentone.setTextView(text);
        fragmentone.setImageView(url);

        list.add(fragmentone);

         fragmentwo = new ShareViewFragmentwo();
        fragmentwo.setTextView(text);
        fragmentwo.setImageView(url);
        list.add(fragmentwo);

        adapter = new MyFragmentStatePagerAdapter(getSupportFragmentManager(), list);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setPageTransformer(false, new GalleryPageTransformer());
        viewPager.setPageMargin(-80);

        mDot1.setEnabled(true);
        mDot2.setEnabled(false);
        mPosition = 0;
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position==0){
                    mDot1.setEnabled(true);
                    mDot2.setEnabled(false);
                    mPosition = 0;

                }
                if (position==1){
                    mDot1.setEnabled(false);
                    mDot2.setEnabled(true);
                    mPosition = 1;

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && MyApplication.getHelper() != null) {//qq分享如果选择留在qq，通过home键退出，再进入app则不会有回调
            MyApplication.getHelper().onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (MyApplication.getHelper() != null) {
            MyApplication.getHelper().onNewIntent(intent);
        }
    }

}
